package com.hejun.bos.common.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;

import com.hejun.bos.domain.base.Area;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	protected T model;
	
	protected Integer page;
	protected Integer rows;
	

	

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Override
	public T getModel() {
		return model;
	}

	public BaseAction() {
		Class<? extends BaseAction> class1 = this.getClass();
		Type type = class1.getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) type;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model = modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			System.out.println("实体类模型初始化异常....");
		}
	}
	
	protected void changeResultToJson(Page<T> result){
		Map<String, Object> map=new HashMap<>();
		map.put("total", result.getTotalElements());
		map.put("rows", result.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
}
